﻿/**
 * @author:dpyang
 * createdate:2013-4-5
 * @describe:用户控制器
 */
Ext.define('WeCompany.controller.Users', {
    extend: 'Ext.app.Controller',

    models: ['User'],
    stores: ['Users', 'Roles'],
    views: ['Users.View'],
    //通过id选择器获得对视图和按钮引用
    refs: [
        { ref: 'UserPanel', selector: '#userPanel' },
        { ref: 'UserView', selector: '#usersView' },
        { ref: 'ButtonUserAdd', selector: '#buttonUserAdd' },
        { ref: 'ButtonUserDelete', selector: '#buttonUserDelete' },
        { ref: 'ButtonUserResetPassword', selector: '#buttonUserResetPassword' }
    ],

    init: function () {
        var me = this,
        panel = me.getUserPanel(),
        view = Ext.widget("usersview");
        panel.add(view);
        //设置视图下的监听事件
        me.getUserView().on('selectionchange', me.onUserSelect, me);
        me.getButtonUserAdd().on('click', me.onAddUser, me);
        me.getUserView().on('edit', me.onEditcomplete, me);
        me.getUserView().on('canceledit', me.onCancelEdit, me);
        me.getButtonUserDelete().on('click', me.onRemoveUser, me);
        me.getButtonUserResetPassword().on('click', me.onResetPassword,me);
        me.control({

        });
    },

    //监听事件
    onUserSelect: function (model, rs) {
        var me = this, length = rs.length;

        //获取按钮并让其显示或者隐藏
        me.getButtonUserDelete().setDisabled(length == 0);
        me.getButtonUserResetPassword().setDisabled(length == 0);
    },

    //先调用cancelEdit取消当前编辑操作，以避免在编辑过程中单击了添加按钮出现问题。
    //接着在Store中添加一条记录。最后调用startEdit方法进入编辑状态。具体代码如下：
    onAddUser: function () {
        var me = this,
          edit = me.getUserView().plugins[0],
          User = me.getUserModel();//getUserModel是定义models配置项时自动生成的方法
        edit.cancelEdit();
        me.getUsersStore().insert(0, new User);
        edit.startEdit(0, 0);
    },

    //如果同步操作，服务器返回success为true，则调用commitChanges方法确认修改，
    //如果失败，则调用rejectChanges方法取消修改，并显示错误信息。
    onEditcomplete: function (editor, e) {
        var me = this;
        me.getUsersStore().sync({
            success: function (e, opt) {
                var me = this;
                me.getUsersStore().commitChanges();
            },
            failure: function (e, opt) {
                var me = this, msg = '';
                me.getUsersStore().rejectChanges();
                Ext.Msg.alert("错误", e.exceptions[0].error);
            },
            scope: me
        });
    },

    onCancelEdit: function () {
        var me = this;

        me.getUsersStore().rejectChanges();
    },

    onRemoveUser: function () {
        var me = this,
            sm = me.getUserView().getSelectionModel();//获取选中的对象
        if (sm.getCount() > 0) {
            var rs = sm.getSelection();//获取选中的对象
            content = ["确定删除以下用户？"];
            for (var i = 0; ln = rs.length, i < ln; i++) {
                content.push(rs[i].data.Username);
            }
            Ext.Msg.confirm("删除用户", content.join("<br/>"), function (btn) {
                if (btn == "yes") {
                    varme = this,
                    rs = me.getUserView().getSelectionModel().getSelection(),//获取选中的行
                    store = me.getUsersStore();//获取store
                    store.remove(rs);//从store移除
                    //同步
                    store.sync({
                        success: function (e, opt) {
                            var me = this;
                            me.getUsersStore().commitChanges();
                        },
                        failure: function (e, opt) {
                            var me = this;
                            me.getUsersStore().rejectChanges()
                            Ext.Msg.alert("发生错误", e.exceptions[0].error);
                        },
                        scope: me
                    });
                }
            }, me)
        } else {
            Ext.Msg.alert('删除用户', '请选择要删除的用户。');
        }
    },

    onResetPassword: function () {
        var me = this,
            rs = me.getUserView().getSelectionModel().getSelection();
        if (rs.length > 0)
        {
            var idList = [];
            for (var i = rs.length - 1; i >= 0; i--) {
                idList.push(rs[i].data.UserName);
            }
            Ext.Ajax.request({
                params: { id: idList },
                url: 'Account/ResetPassword',
                method: 'POST',
                success: function (response, opt) {
                    var obj = Ext.decode(response.responseText);
                    if (obj) {
                        if (obj.success) {
                            Ext.Msg.alert('提示信息', '重置成功!');
                            return;
                        } else {
                            Ext.Msg.alert('失败',obj.Msg);
                        }
                    }

                },
                failure: function (response, opt) {
                    Ext.Msg.alert('失败', response.responseText);
                }
            });
        }
    }
})